/**
* User services
*/
app.factory("userServices", function($http, toolsServices) {
	var users = [];
	return {
		initialize: function(callback) {
			console.log('userServices - get all users');
			$http.post(app.serviceBase, { "service" : "getUsers" }).
			success(function(data, status) 
			{
				console.info("getUsers - call success"); 
				if(data.isFailed == true)
				{
					throw data.exception;
				}
				users = data.response;
				callback(users);
			})
			.
			error(function(data, status) 
			{
				console.error("getUsers - call failed"); 	
				throw status + ' : ' + data;		
			});
		},
		allUsers: function(callback) {
		  callback(users);
		},
		isLoggedIn : function() {
			return (toolsServices.getCurrentUser() != undefined) ? true : false;
		},
		getLoggedUser: function() {
			return toolsServices.getCurrentUser();
		},
		authenticate: function(callback, login, password) {
			$http.post(app.serviceBase, { "service" : "authenticate", "login" : login, "password" : password }).
			success(function(data, status) 
			{
				console.info("userServices.authenticate - call success"); 
				if(data.isFailed == true) {
					console.info("userServices.authenticate - error : " + data.exception); 
				}
				else {
					toolsServices.setCurrentUser(data.response);
				}
				callback(data);
			})
			.
			error(function(data, status) 
			{
				console.error("authenticate - call failed"); 	
				throw status + ' : ' + data;		
			});
		},
		logout: function() {
			console.info("userServices.logout - call"); 
			toolsServices.removeCurrentUser();
		}
	};
});